package fsmonitor

import "time"

type Monitor struct {
	running   bool
	interval  time.Duration
	observers []*Observer
}

func (m *Monitor) AddObserver(observer *Observer) {
	m.observers = append(m.observers, observer)
}

func (m *Monitor) run() {
	for {
		if !m.running {
			break
		}
		for _, observer := range m.observers {
			observer.run()
		}
		time.Sleep(time.Duration(m.interval))
	}

}

func (m *Monitor) Start() {
	if m.running {
		return
	}
	m.running = true
	go m.run()
}

func NewMonitor(interval time.Duration) *Monitor {
	return &Monitor{interval: interval}
}
